/*
 * Copyright 2017 Baiyyy
 * @(#)PsZcdController.java
 */

package com.byyy.ccts.modules.cc.controller;

import com.byyy.ccts.core.common.controller.BaseCRUDController;
import com.byyy.ccts.core.model.AjaxJson;
import com.byyy.ccts.core.security.shiro.authz.annotation.RequiresPathPermission;
import com.byyy.ccts.core.utils.DateUtils;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.cc.entity.ccts.PsZcdHz;
import com.byyy.ccts.modules.cc.entity.ccts.PsZcdMx;
import com.byyy.ccts.modules.cc.service.IPsZcdMxService;
import com.byyy.ccts.modules.cc.service.IPsZcdService;
import com.byyy.ccts.modules.common.annotation.LogRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by DELL on 2018-6-26.
 */
@Controller
@RequestMapping("${admin.url.prefix}/cc/zcd")
@RequiresPathPermission("cc:zcd")
public class PsZcdController extends BaseCRUDController<PsZcdHz, String> {

    @Autowired
    private IPsZcdService psZcdService;

    @Autowired
    private IPsZcdMxService psZcdMxService;

    /**
     * 发车确认
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "facConfirm", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "装车确认")
    public AjaxJson facConfirm(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        String[] zcdNos = request.getParameterValues("zcdNos");
        ajaxJson.success("发车成功");
        String msg = this.psZcdService.facConfirm(new ArrayList(Arrays.asList(zcdNos)));
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
        }
        return ajaxJson;
    }

    /**
     * 配送交接页面
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="handOverEdit", method = RequestMethod.GET)
    public String handOverEdit(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdNo = request.getParameter("zhuangcdNo");
        model.addAttribute("zhuangcdNo", zhuangcdNo);
        model.addAttribute("zcdMxList", this.psZcdMxService.selectSimpleListByZcdNo(zhuangcdNo));
        return display("handOverEdit");
    }

    /**
     * 配送交接前的验证工作
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "beforeHandOver", method = {RequestMethod.POST})
    @ResponseBody
    public AjaxJson beforeHandOver(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        String[] danjNos = request.getParameterValues("danjNos");
        String zcdNo = request.getParameter("zcdNo");
        Date handOverDate = DateUtils.toDate(request.getParameter("handOverDate"), "yyyy-MM-dd HH:mm:ss");
        String msg = this.psZcdService.beforeHandOver(zcdNo, handOverDate, Arrays.asList(danjNos));
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
        }
        else {
            msg = this.psZcdService.handOver(zcdNo, handOverDate, Arrays.asList(danjNos));
            if (!StringUtils.isEmpty(msg)) {
                // 不允许操作的消息
                ajaxJson.setRet(-2);
                ajaxJson.setMsg(msg);
            }
        }
        return ajaxJson;
    }

    /**
     * 配送交接
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "handOver", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "配送交接")
    public AjaxJson handOver(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        String[] danjNos = request.getParameterValues("danjNos");
        String zcdNo = request.getParameter("zcdNo");
        Date handOverDate = DateUtils.toDate(request.getParameter("handOverDate"), "yyyy-MM-dd HH:mm:ss");
        ajaxJson.success("签收确认完毕");
        String msg = this.psZcdService.handOver(zcdNo, handOverDate, Arrays.asList(danjNos));
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
        }
        return ajaxJson;
    }

    /**
     * 冷藏车关联页面
     * @param model
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="carRelationEdit", method = RequestMethod.GET)
    public String carRelationEdit(Model model, HttpServletRequest request, HttpServletResponse response) {
        String zhuangcdNos = request.getParameter("zhuangcdNos");
        List<PsZcdMx> zcdMxList = this.psZcdMxService.selectByZcdNos(Arrays.asList(zhuangcdNos.split(",")));
        model.addAttribute("zhuangcdNos", zhuangcdNos);
        model.addAttribute("zcdMxList", zcdMxList);
        model.addAttribute("car", zcdMxList.get(0).getBoxSn());
        model.addAttribute("pointOne", zcdMxList.get(0).getPointone());
        model.addAttribute("pointTwo", zcdMxList.get(0).getPointtwo());
        return display("carRelationEdit");
    }

    /**
     * 冷藏车关联
     *
     * @throws java.io.IOException
     */
    @RequestMapping(value = "carRelation", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "冷藏车关联")
    public AjaxJson carRelation(String zhuangcdNos, String chillCarId, String measurepointOne, String measurepointTwo) {
        AjaxJson ajaxJson = new AjaxJson();
        String msg = this.psZcdService.carRelation(Arrays.asList(zhuangcdNos.split(",")), chillCarId, measurepointOne, measurepointTwo);
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
        }
        return ajaxJson;
    }

    /**
     * 作废
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "cancel", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "作废单据")
    public AjaxJson cancel(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        String[] zcdNos = request.getParameterValues("zcdNos");
        ajaxJson.success("单据作废完毕");
        String msg = this.psZcdService.cancel(Arrays.asList(zcdNos));
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
        }
        return ajaxJson;
    }

    /**
     * 同步装车单
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "syncZcd", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "同步装车单")
    public AjaxJson syncZcd(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        String zcdNo = request.getParameter("zcdNo");
        ajaxJson.success("装车单同步完毕");
        String msg = this.psZcdService.syncZcd(zcdNo);
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
        }
        return ajaxJson;
    }

    /**
     * 装车单信息反馈
     *
     * @param request
     * @param response
     * @throws java.io.IOException
     */
    @RequestMapping(value = "back", method = {RequestMethod.POST})
    @ResponseBody
    @LogRecord(title = "装车启运信息反馈")
    public AjaxJson back(HttpServletRequest request, HttpServletResponse response) {
        AjaxJson ajaxJson = new AjaxJson();
        String[] zcdNos = request.getParameterValues("zcdNos");
        ajaxJson.success("装车启运信息反馈完毕");
        String msg = this.psZcdService.back(Arrays.asList(zcdNos));
        if (!StringUtils.isEmpty(msg)) {
            ajaxJson.fail(msg);
        }
        return ajaxJson;
    }

    /**
     * 托运装车单冷链追溯
     * @return
     */
    @RequestMapping(value="consignment", method = RequestMethod.GET)
    public String zcdDsf() {
        return display("listCons");
    }

    /**
     * 托运装车单冷链追溯
     * @return
     */
    @RequestMapping(value="zcdNotConfirm", method = RequestMethod.GET)
    public String zcdNotConfirm() {
        return display("listNotConfirm");
    }

}
